//
//  DDRegisterViewController.m
//  DDMusic
//
//  Created by rimi on 16/11/6.
//  Copyright © 2016年 FengBaoFamily. All rights reserved.
//
/*******************这是注册页面
该页面视图tag值从100开始
************************/
#import "DDRegisterViewController.h"
#import <Masonry.h>
#import "UILabel+DDUILabelCategory.h"
#import "UIButton+DDUIButtonCategory.h"
#import "UITextField+DDUITextFieldCategory.h"
#import <AVOSCloudSNS.h>
#import "DDAlertView.h"
@interface DDRegisterViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField* accountTextField;//账号输入框
@property (nonatomic,strong) UITextField* passwordTextFiled;//密码输入框
@property (nonatomic,strong) UITextField* emailTextField;//电子邮箱输入框
@property (nonatomic,strong) UIButton* accountClearBtn;//账号输入框的清除按钮
@property (nonatomic,strong) UIButton* emailClearBtn;//邮箱输入框的清除按钮
@end

@implementation DDRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutUI];

}

#pragma mark 布局UI
- (void)layoutUI{
    self.view.backgroundColor = [UIColor whiteColor];
//背景视图
    UIImageView* bgImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"registBgImage"]];
    bgImageView.frame = self.view.bounds;
    [self.view addSubview:bgImageView];
//返回按钮
    UIButton* returnBtn =
    [UIButton dd_creatButtonTitle:nil buttonType:UIButtonTypeCustom font:0 titieColor:nil bgColor:nil nomalImage:nil bgImage:@"error"];
    [self.view addSubview:returnBtn];
    [returnBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@20);
        make.left.equalTo(@25);
        make.top.equalTo(@25);
    }];
    [returnBtn addTarget:self action:@selector(returnBtnClicked) forControlEvents:(UIControlEventTouchUpInside)];
//中间应用文本
    UILabel* midLabel = [UILabel dd_CreatLabelWithTitle:@"Dai Dai Music" font:0 TitleColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentCenter];
    midLabel.font = [UIFont fontWithName:@"Bradley Hand" size:50];
    midLabel.adjustsFontSizeToFitWidth = YES;
    [self.view addSubview:midLabel];
    __weak  DDRegisterViewController* weakSelf = self;
    [midLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.top.equalTo(@100);
        make.left.equalTo(@40);
        make.right.equalTo(@(-40));
        
        
    }];
//注册账号label左视图
    UILabel* accountLetfLabel = [UILabel dd_CreatLabelWithTitle:@"注册账号:" font:20 TitleColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentLeft];
    accountLetfLabel.frame = CGRectMake(0, 0, 100, 44);
//账号清除按钮
    self.accountClearBtn = [UIButton dd_creatButtonTitle:nil buttonType:(UIButtonTypeCustom) font:0 titieColor:nil bgColor:nil nomalImage:@"error" bgImage:nil];
    self.accountClearBtn.frame = CGRectMake(0, 0, 44, 44);
    [self.accountClearBtn addTarget:self action:@selector(clearBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
//accountTextField
    self.accountTextField = [UITextField dd_creatTextFieldWithPlaceHolder:nil font:18 textColor: [UIColor whiteColor] leftView:accountLetfLabel isShowClearButton:NO rightView:self.accountClearBtn];

    self.accountTextField.delegate = self;
    self.accountTextField.keyboardType =  UIKeyboardTypeASCIICapable;
  [self.view addSubview:self.accountTextField];
    [self.accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midLabel.mas_bottom).offset(50);
        make.height.equalTo(@44);
        make.left.equalTo(@50);
        make.right.equalTo(@(-50));
    }];

//注册密码label左视图
    UILabel* passwordLetfLabel = [UILabel dd_CreatLabelWithTitle:@"注册密码:" font:20 TitleColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentLeft];
    passwordLetfLabel.frame = CGRectMake(0, 0, 100, 44);
//passwordTextFiled
    self.passwordTextFiled = [UITextField dd_creatTextFieldWithPlaceHolder:nil font:18 textColor:[UIColor whiteColor] leftView:passwordLetfLabel isShowClearButton:NO rightView:nil];
    self.passwordTextFiled.keyboardType =  UIKeyboardTypeASCIICapable;
    self.passwordTextFiled.delegate = self;
    [self.view addSubview:self.passwordTextFiled];
    [self.passwordTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.accountTextField.mas_bottom).offset(30);
        make.height.equalTo(self.accountTextField.mas_height);
        make.left.equalTo(self.accountTextField.mas_left);
        make.right.equalTo(self.accountTextField.mas_right);
    }];
    
//注册电子邮箱label左视图
    UILabel* emailLetfLabel = [UILabel dd_CreatLabelWithTitle:@"电子邮箱:" font:20 TitleColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentLeft];
    emailLetfLabel.frame = CGRectMake(0, 0, 100, 44);
//邮箱清除按钮
    self.emailClearBtn = [UIButton dd_creatButtonTitle:nil buttonType:(UIButtonTypeCustom) font:0 titieColor:nil bgColor:nil nomalImage:@"error" bgImage:nil];
    self.emailClearBtn.frame = CGRectMake(0, 0, 44, 44);
    [self.emailClearBtn addTarget:self action:@selector(clearBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
//emailTextField
    self.emailTextField = [UITextField dd_creatTextFieldWithPlaceHolder:nil font:18 textColor:[UIColor whiteColor] leftView:emailLetfLabel isShowClearButton:NO rightView:self.emailClearBtn];
    self.emailTextField.keyboardType = UIKeyboardTypeASCIICapable;
    self.emailTextField.delegate = self;
    [self.view addSubview:self.emailTextField];
    [self.emailTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordTextFiled.mas_bottom).offset(30);
        make.height.equalTo(self.passwordTextFiled.mas_height);
        make.left.equalTo(self.passwordTextFiled.mas_left);
        make.right.equalTo(self.passwordTextFiled.mas_right);
    }];
//注册按钮
    UIButton* registerBtn = [UIButton dd_creatButtonTitle:@"注册" buttonType:(UIButtonTypeRoundedRect) font:18 titieColor:[UIColor whiteColor] bgColor:nil nomalImage:nil bgImage:nil];
    [registerBtn addTarget:self action:@selector(registerBtnClicked) forControlEvents:(UIControlEventTouchUpInside)];
    registerBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    registerBtn.layer.borderWidth = 1;
    [self.view addSubview:registerBtn];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@100);
        make.height.equalTo(@30);
        make.right.equalTo(self.accountTextField.mas_right);
        make.top.equalTo(self.emailTextField.mas_bottom).offset(20);
    }];
//第三方登录视图
    UIView* thirdPartyLoginView = [[UIView alloc]init];
    [self.view addSubview:thirdPartyLoginView];
    [thirdPartyLoginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.height.equalTo(@100);
        make.bottom.equalTo(@0);
    }];

    UILabel* label = [UILabel dd_CreatLabelWithTitle:@" 第三方登录方式 " font:15 TitleColor:[UIColor whiteColor] TextAlignment:NSTextAlignmentCenter];
    [thirdPartyLoginView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@0);
        make.top.equalTo(@0);
    }];
    UIView* leftView = [[UIView alloc]init];
    leftView.backgroundColor = [UIColor whiteColor];
    [thirdPartyLoginView addSubview:leftView];
    [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@40);
        make.right.equalTo(label.mas_left).offset(2);
        make.height.equalTo(@1);
        make.centerY.equalTo(label.mas_centerY);
    }];
    UIView* rightView = [[UIView alloc]init];
    rightView.backgroundColor = [UIColor whiteColor];
    [thirdPartyLoginView addSubview:rightView];
    [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(label.mas_right).offset(2);
        make.right.equalTo(@(-40));
        make.height.equalTo(@1);
        make.centerY.equalTo(label.mas_centerY);
    }];
//qq第三方分享按钮
    UIButton* qqBtn = [UIButton dd_creatButtonTitle:nil buttonType:(UIButtonTypeCustom) font:0 titieColor:nil bgColor:nil nomalImage:nil bgImage:@"qq"];
    UILabel* qqLabel = [UILabel dd_CreatLabelWithTitle:@"QQ" font:11 TitleColor:[UIColor whiteColor] TextAlignment:(NSTextAlignmentCenter)];
    qqBtn.tag = 102;
    [qqBtn addTarget:self action:@selector(thirdPartyLoginBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    [thirdPartyLoginView addSubview:qqBtn];
    [thirdPartyLoginView addSubview:qqLabel];
    [qqBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@35);
        make.centerX.equalTo(@0);
        make.centerY.equalTo(@0);
    }];
    [qqLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(qqBtn.mas_centerX);
        make.top.equalTo(qqBtn.mas_bottom).offset(2);
    }];
    
//微信第三方分享按钮
    UIButton* weixinBtn = [UIButton dd_creatButtonTitle:nil buttonType:(UIButtonTypeCustom) font:0 titieColor:nil bgColor:nil nomalImage:nil bgImage:@"weiXin"];
    weixinBtn.tag = 101;
    [weixinBtn addTarget:self action:@selector(thirdPartyLoginBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    UILabel* weixinLabel = [UILabel dd_CreatLabelWithTitle:@"微信" font:11 TitleColor:[UIColor whiteColor] TextAlignment:(NSTextAlignmentCenter)];
    [thirdPartyLoginView addSubview:weixinBtn];
    [thirdPartyLoginView addSubview:weixinLabel];
    [weixinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(qqBtn.mas_width);
        make.top.equalTo(qqBtn.mas_top);
        make.right.equalTo(qqBtn.mas_left).offset(-40);
    }];
    [weixinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weixinBtn.mas_centerX);
        make.top.equalTo(weixinBtn.mas_bottom).offset(2);
    }];
//新浪微博
    UIButton* xinLangBtn = [UIButton dd_creatButtonTitle:nil buttonType:(UIButtonTypeCustom) font:0 titieColor:nil bgColor:nil nomalImage:nil bgImage:@"xinLang"];
    xinLangBtn.tag = 103;
    [xinLangBtn addTarget:self action:@selector(thirdPartyLoginBtnClicked:) forControlEvents:(UIControlEventTouchUpInside)];
    UILabel* xinLangLabel  = [UILabel dd_CreatLabelWithTitle:@"新浪微博" font:11 TitleColor:[UIColor whiteColor] TextAlignment:(NSTextAlignmentCenter)];
    [thirdPartyLoginView addSubview:xinLangBtn];
    [thirdPartyLoginView addSubview:xinLangLabel];
    [xinLangBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(qqBtn.mas_width);
        make.top.equalTo(qqBtn.mas_top);
        make.left.equalTo(qqBtn.mas_right).offset(40);

    }];
    [xinLangLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(xinLangBtn.mas_centerX);
        make.top.equalTo(xinLangBtn.mas_bottom).offset(2);
    }];
    
}

#pragma mark 逻辑操作部分
#pragma mark 返回按钮的回调方法
- (void)returnBtnClicked{
    [self.accountTextField resignFirstResponder];
    [self.passwordTextFiled resignFirstResponder];
    [self.emailTextField resignFirstResponder];
    [self dismissViewControllerAnimated:YES completion:nil];

}
#pragma mark 注册按钮的回调方法
- (void)registerBtnClicked{
    if ([self.accountTextField.text  isEqual: @""]) {
        [[DDAlertView sharedDDAlertView]showAlertViewWithTitle:@"注册账号不能为空!"];
        return;
    }
    if ([self.passwordTextFiled.text  isEqual: @""]) {
        [[DDAlertView sharedDDAlertView]showAlertViewWithTitle:@"注册密码不能为空!"];
        return;
    }
    if ([self.emailTextField.text  isEqual: @""]) {
        [[DDAlertView sharedDDAlertView]showAlertViewWithTitle:@"注册邮箱不能为空!"];
        return;
    }
///开始注册
    AVUser *user = [AVUser user];// 新建 AVUser 对象实例
    user.username = self.accountTextField.text;// 设置用户名
    user.password =  self.passwordTextFiled.text;// 设置密码
    user.email = self.emailTextField.text;// 设置邮箱
    
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
         [[DDAlertView sharedDDAlertView]showAlertViewWithTitle:@"注册成功!"];
            [self returnBtnClicked];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"DDRegisterViewControllerRegistSucceed" object:@{@"userName":self.accountTextField.text,@"passWord":self.passwordTextFiled.text}];
        } else {
            if (error.code == 125) {
              [[DDAlertView sharedDDAlertView]showAlertViewWithTitle:@"邮箱格式不正确!"];
            }
            if (error.code == 202) {
                   [[DDAlertView sharedDDAlertView]showAlertViewWithTitle:@"该账号已被使用!"];
            }
            if (error.code ==203) {
                      [[DDAlertView sharedDDAlertView]showAlertViewWithTitle:@"邮箱已被使用!"];
            }
            if (error.code ==214) {
                      [[DDAlertView sharedDDAlertView]showAlertViewWithTitle:@"该手机号码已被使用"];
            }
        }
    }];

}
#pragma mark 隐藏系统的状态栏
- (BOOL)prefersStatusBarHidden{
    return YES;
    
}
#pragma mark 回收键盘的方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.accountTextField resignFirstResponder];
    [self.passwordTextFiled resignFirstResponder];
    [self.emailTextField resignFirstResponder];
}
#pragma mark 键盘清除按钮的回调方法
- (void)clearBtnClicked:(UIButton*)clearBtn{
    if (clearBtn == self.accountClearBtn) {
        self.accountTextField.text = @"";
    }else if(clearBtn ==self.emailClearBtn){
    self.emailTextField.text = @"";
    }
}
#pragma mark 文本输入框的代理方法
///限制文本输入框只输入英文和数字的方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSUInteger strlength = textField.text.length-range.length+string.length;

    if (strlength<=30 && [self filtrationTextFieldText:string textField:textField]) {
        return YES;
    }else{
        return NO;
    }
}
- (BOOL)filtrationTextFieldText:(NSString*)str textField:(UITextField*)textField{
    BOOL result = YES;
    NSCharacterSet* tmpSet;
    if (textField == self.emailTextField) {
     tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM@."];
    }else{
  tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"];
    }
   
    int i = 0;
    while (i<str.length) {
        NSString* string = [str substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            result = NO;
            break;
        }
        i++;
        
        
    }
    if (textField == self.passwordTextFiled) {
        result =YES;
    }
    return result;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.passwordTextFiled) {
        self.passwordTextFiled.text = @"";
    }
}
#pragma mark 第三方登录按钮的回调方法
- (void)thirdPartyLoginBtnClicked:(UIButton*)button{
  
    switch (button.tag) {
        case 101:
           //微信第三方登录
                       break;
            case 102:
            //qq第三方登录


            break;
        default:
            //新浪微博第三方登录
           
            break;
    }
   
}
@end
